package wyh.controller.User;

import com.fasterxml.jackson.databind.ObjectMapper;
import wyh.pojo.User;
import wyh.service.UserService;
import wyh.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//9666666666666666666 ok
@WebServlet("/User/get2")
public class GetAllServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserService();
        List<User> userList = service.ServicegetAll();

        // 过滤出 user_permission 为 customer 的用户
        List<User> rootUserList = new ArrayList<>();
        for (User user : userList) {
            if ("customer".equals(user.getUser_permission())) {
                rootUserList.add(user);
            }
        }

        Result result = new Result(200, "查询成功", rootUserList);
        ObjectMapper ojmapper = new ObjectMapper();
        String json = ojmapper.writeValueAsString(result);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }
}